﻿package com.bizmlib.ui.scrolling
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	
	
	public class AbstractSlider extends MovieClip
	{
		private static const STATE_UP:Number = 0;
		private static const STATE_DN:Number = 1;
		
		protected var state:Number;
		
		
		public function AbstractSlider()
		{
			init();
		}
		
		
		override public function set mouseEnabled(value:Boolean):void
		{
			super.mouseEnabled = value;
			
			if(!mouseEnabled && state==STATE_DN)
			{
				release();
			}
		}
		override public function get mouseEnabled():Boolean
		{
			return super.mouseEnabled;
		}
		
		
		protected function init():void
		{
			state = STATE_UP;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		
		private function removedFromStage(event:Event):void
		{
			this.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
			
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
		}
		protected function mouseDown(event:MouseEvent):void
		{
			//trace(this + ".mouseDown(" + arguments + ")");
			
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			
			state = STATE_DN;
			
			doMouseDown();
		}
		protected function mouseUp(event:MouseEvent):void
		{
			//trace(this + ".mouseUp(" + arguments + ")");
			
			release();
		}
		protected function mouseMove(event:MouseEvent):void
		{
			dispatchEvent(new ScrollerEvent(ScrollerEvent.SLIDER_MOVE));
			
			doMouseMove()
		}
		protected function release():void
		{
			state = STATE_UP;
			
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			
			dispatchEvent(new ScrollerEvent(ScrollerEvent.SLIDER_RELEASE));
			
			doMouseUp();
		}
		
		
		protected function doMouseDown():void
		{
			//trace(this + ".doMouseDown(" + arguments + ")");
		}
		protected function doMouseUp():void
		{
			//trace(this + ".doMouseUp(" + arguments + ")");
		}
		protected function doMouseMove():void
		{
			//trace(this + ".doMouseMove(" + arguments + ")");
		}
		
		
		override public function toString():String
		{
			return "[com.bizmlib.ui.scrolling.AbstractSlider]";
		}
	}
}